<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ include file="../include.jsp" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
  	<meta http-equiv="Content-Type" content="multipart/form-data; charset=utf-8" />
    <title>列表</title>
    <script type="text/javascript">
    	 $(function(){
    		$("#dg").datagrid({    
			    url:'<%=path%>/releaseTask/findAllByType?type=1',
				method:"get",
			    columns:[[
			        {field:'taskName',title:'任务名称',width:100,align:'center'},
                    {field:'time',title:'发布时间',width:120,align:'center'},
                    {field:'url',title:'发布路径',width:400,align:'center'},
                    {field:'state',title:'状态',width:100,align:'center',formatter: function(v,r,i){return statusTask(v);}},
                    {field:'users',title:'任领人',width:100,align:'center',formatter: function(v,r,i){return r.users==null?"":(r.users.userName==null?"":r.users.userName);}},
                    {field:'updateUrl',title:'任领人发布',width:400,align:'center'},
                    {field:'updateTime',title:'任领人发布时间',width:120,align:'center'},
                    {field:'remark',title:'备注信息',width:100,align:'center'}
			    ]],
			    pagination:true,//底部显示分页组件 
			    singleSelect:true,//设置单选
			    toolbar: [{
					iconCls: 'icon-add',
					text:'发布任务',
					handler: function(){
						//当点击添加时,让隐藏的对话框弹出
						$("#dd").dialog("open");
					}
				},'-',{
					iconCls: 'icon-remove',
					text:'删除',
					handler: function(){
						//判断是否选中记录
						var row=$("#dg").datagrid('getSelected');
						if(row==null){//未选择
							$.messager.show({
								title:'提示消息',
								msg:'请选择要删除的发布任务信息!',
								timeout:3000,
								showType:'slide'
							});
						}else{//已选择
							var id=row.id;
							var state=row.state;//获取任务状态
							if(state!=1){
								$.messager.show({
									title:'提示消息',
									msg:'对不起发布任务信息已有人认领操作，无法删除!',
									timeout:3000,
									showType:'slide'
								});
								return false;
							}
							//确认是否要删除
							$.messager.confirm('确认对话框', '您确定要删除吗？', function(r){
								if(r){
								  	 $.post("<%=path%>/releaseTask/del", {id:id},
									   		function(data){
									     	if(data==true){//删除成功
									     		//刷新数据表格
									     		$("#dg").datagrid("reload");
									     		//提示删除成功
									     		$.messager.show({
													title:'我的消息',
													msg:"删除成功!",
													timeout:3000,
													showType:'slide'
												});
									     	}else{
									     		//提示删除成功
									     		$.messager.show({
													title:'我的消息',
													msg:"删除失败!",
													timeout:3000,
													showType:'slide'
												});
									     	}
									   },"json");
								}
							});
						}
					}
				},'-',{
					// iconCls: 'icon-ok',
					// text:'提交',
					handler: function(){
						//判断是否选中记录
						var row=$("#dg").datagrid('getSelected');
						if(row==null){//未选择
							$.messager.show({
								title:'提示消息',
								msg:'请选择要提交的免险!',
								timeout:3000,
								showType:'slide'
							});
						}else if(row.status!=1){
							$.messager.show({
								title:'提示消息',
								msg:'该免险已提交!',
								timeout:3000,
								showType:'slide'
							});
						}else{
							//提交免险
							var id=row.id;
							//确认是否要提交
							$.messager.confirm('确认对话框', '您确定要提交吗？', function(r){
								if(r){
								  	 $.post("<%=path%>/freeIns/submitFreeIns.do", {id:id},
									   		function(data){
									     	if(data==true){//提交成功
									     		//刷新数据表格
									     		$("#dg").datagrid("reload");
									     		//提示提交成功
									     		$.messager.show({
													title:'我的消息',
													msg:"提交成功!",
													timeout:3000,
													showType:'slide'
												});
									     	}else{
									     		//提示提交成功
									     		$.messager.show({
													title:'我的消息',
													msg:"提交失败!",
													timeout:3000,
													showType:'slide'
												});
									     	}
									   },"json");
								}
							});
						}
					}
				}]    
			});
			/****************************************发布任务操作*******************************************************/
			//给保存免险弹出框的关闭按钮添加单击事件
			$("#saveCloseBtn").click(function(){
				//当点击添加时,话框关闭
				$("#dd").dialog("close");
			});
			//给保存发布任务弹出框的保存按钮添加单击事件
			$("#saveBtn").click(function(){
				var formData = new FormData();
				if ($('#uploadFile')[0].files[0] == null) {
					$.messager.show({title:'我的消息', msg:"请先选择要发布的文件", timeout:3000, showType:'slide'});
					return false;
				}
				var taskName=$('#taskName').val();
				if (taskName== null || taskName=="") {
					$.messager.show({title:'我的消息', msg:"请输入发布任务名称", timeout:3000, showType:'slide'});
					return false;
				}
				var remark=$('#remark').val();
				if (remark== null || remark=="") {
					$.messager.show({title:'我的消息', msg:"请输入发布任务备注信息", timeout:3000, showType:'slide'});
					return false;
				}
				formData.append('uploadFile', $('#uploadFile')[0].files[0]);
				formData.append('taskName', taskName);
				formData.append('remark', $('#remark').val());
				formData.append('type', 1);
				$.ajax({
					// enctype: 'multipart/form-data',
					type: 'POST',
					cache: false,
					url: '<%=path%>/releaseTask/uploadTask',
					// data: {taskName:taskName,remark:remark,uploadFile:uploadFile,type:type},
					data: formData,
					processData: false,
					contentType: false,
					dataType: "json",
					success: function(result) {
						$.messager.show({title:'我的消息', msg:result.msg, timeout:3000, showType:'slide'});
						if(result.flag==true){$("#dd").dialog("close");$("#dg").datagrid("reload");}
					}
				});
			});
		   /****************************************发布任务操作*******************************************************/
    	
    	 });
    </script>
  </head>
  
  <body>
    <table id="dg"></table>
    <!-- 添加发布的对话框 -->
    <div id="dd" class="easyui-dialog" title="发布任务" style="width:310px;height:223px;"
        data-options="iconCls:'icon-save',resizable:true,modal:true,buttons:'#bb',closed:true">
		<br/>
    	<form id="myForm" enctype="multipart/form-data" method="post">
    		任务名称:&nbsp;<input type="text" id="taskName" name="taskName"/><br/><br/>
    		上传数据:&nbsp;<input type="file" id="uploadFile" name="uploadFile"/><br/><br/>
    		备注信息:&nbsp;<input type="text" id="remark" name="remark"/><br/><br/>
    	</form>  
	</div>
	<div id="bb">
		<a id="saveBtn" href="#" class="easyui-linkbutton">保存</a>
		<a id="saveCloseBtn" href="#" class="easyui-linkbutton">关闭</a>
	</div>
	
	  
  </body>
</html>
